package com.bsy.videostation.viewmodel

import androidx.lifecycle.MutableLiveData
import com.bsy.common.viewmodel.BaseViewModel
import com.bsy.framework.ext.data
import com.bsy.videostation.model.VideoEntity
import com.bsy.videostation.model.VideoListEntity
import com.bsy.videostation.model.res.VideoInfo
import com.bsy.videostation.repository.MainRepository
import com.bsy.videostation.view_type.VideoEntityType
import com.google.gson.JsonParser
import com.google.gson.JsonSyntaxException

/**
 * Created by Sincerity on 2024/5/22.
 * Copyright
 * 描述：
 */
class VideoViewModel : BaseViewModel() {
    private val mainRepository by lazy { MainRepository() }
    val videoList = MutableLiveData<MutableList<VideoListEntity>?>()
    fun getAppInfoList(categorySelectId: String?) {
        launchUI(responseBlock = {
            val categoryList = mainRepository.getVideoListById(categorySelectId.data())
            categoryList?.let { buildVideoList(it) }
        })
    }

    private fun buildVideoList(response: VideoInfo) {
        val videos = mutableListOf<VideoEntity>()
        //拿取封面
        response.panelReleases?.forEach {
            it.video_contents?.forEach { videoInfo ->
                val id = videoInfo.id.data()
                //    拿到视屏相关信息
                val video = videoInfo.block?.block_content_info
                //是否上架中
                if (video?.content_status == 1) {
                    var cover: String? = null
                    var isVertical: Boolean? = false
                    //封面信息
                    if (video.imgs?.allPoster != null && video.imgs.allPoster.isNotEmpty()) {
                        val coverInfo = video.imgs.allPoster[0]
                        //封面
                        cover = coverInfo?.url.data()
                        //封面是横屏还是竖屏
                        isVertical = coverInfo?.style.data() == "v"
                    }
                    //标题
                    val title = video.title.data()
                    val isShowTitle = video.show_title.data() == 0
                    var appUrl: String? = null
                    var appName: String? = null
                    //来源图标
                    if (video.app_info != null) {
                        appUrl = video.app_info.app_url.data()
                        appName = video.app_info.supplier.data()
                    }
                    //详情url
                    val detail = video.action.data()
                    var url: String? = null
                    if (detail.isNotEmpty()) {
                        try {
                            // 使用 Gson 的 JsonParser 解析 detail 字符串为 JsonObject
                            val obj = JsonParser.parseString(detail).asJsonObject
                            // 直接获取 obj 中的 "url" 属性
                            if (obj.has("url")) {
                                url = obj.get("url").asString
                            }
                        } catch (e: JsonSyntaxException) {
                            e.printStackTrace()
                        } catch (e: IllegalStateException) {
                            e.printStackTrace()
                        }
                    }
                    videos.add(
                        VideoEntity(
                            id,
                            cover,
                            isVertical.data(),
                            title,
                            url.data(),
                            isShowTitle,
                            appUrl,
                            appName
                        )
                    )
                }
            }
        }
        val homeEntities: MutableList<VideoListEntity> = mutableListOf()
        var index = 0
        var isLandscapeNext = true // 用于交替判断

        while (index < videos.size) {
            val homeEntity = VideoListEntity()
            val entity = videos[index]
            entity.index = index
            if (isLandscapeNext) {
                // 从 videos 中取 5 个元素存入 hItem
                repeat(5) {
                    if (index < videos.size) {
                        homeEntity.hItem.add(entity)
                        homeEntity.viewType = VideoEntityType.PORTRAIT.value
                        index++
                    }
                }
            } else {
                // 从 videos 中取 3 个元素存入 vItem
                repeat(3) {
                    if (index < videos.size) {
                        homeEntity.vItem.add(entity)
                        homeEntity.viewType = VideoEntityType.LANDSCAPE.value
                        index++
                    }
                }
            }
            // 交替变换
            isLandscapeNext = !isLandscapeNext

            // 将 homeEntity 添加到 homeEntities
            homeEntities.add(homeEntity)
        }
        videoList.value = homeEntities
    }
}